﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.IRepository;
using DAL.Admin;
using Entities;
using BLL.BLL;
using WindowsForms.Admin;

namespace WindowsForms
{

    public partial class FormNhanVien : Form
    {
        public bool State = true;
        int nums = 15;
        int page = 1;
        int tong;
        public IGiaiDauRepository gd = new BLTGiaiDauRepository();
        public INhanVienRepository nv = new BLTNhanVienRepository();
        public ITaiKhoanRepository tki = new BLTTaiKhoanRepository();
        public IBoPhanRepository bp = new BLTBoPhanRepository();
        public IQuanLiTaiKhoanRepository qltk = new BLTQuanLiTaiKhoanRepository();
        public IDiaChiRepository dchi = new BLTDiaChiRepositoy();
        public FormNhanVien()
        {
            InitializeComponent();

            dagrnv.AutoGenerateColumns = false;
        }
        private void LoadNhanVien(List<NhanVien> ds)
        {

            DiaChi d = new DiaChi();
            string k = "";
            dagrnv.DataSource = ds;

            for (int i = 0; i < dagrnv.RowCount; i++)
            {
                string id = dagrnv.Rows[i].Cells[1].Value.ToString();
                d = dchi.LayTheoId(id);
                k = "";
                if (d != null)
                {
                    k += d.SoNha + " " + d.TenDuong + " " + d.QuanHuyen + " " + d.TinhTP;
                    dagrnv.Rows[i].Cells[8].Value = k;

                }
                dagrnv.Rows[i].Cells[0].Value = i + 1;
            }
            tbdl.Text = page + "/ " + tong;

        }

        private void FormNhanVien_Load(object sender, EventArgs e)
        {

        }

        private void dagrnv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            try
            {


                NhanVien nhanvien = new NhanVien();
                DiaChi d = new DiaChi();
                int hang = e.RowIndex;
                string id = dagrnv.Rows[hang].Cells[1].Value.ToString();
                if (e.ColumnIndex == 9)
                {
                    nhanvien = nv.LayTheoId(id);
                    d = dchi.LayTheoId(id);

                    dchi.XoaDiaChi(d);
                    nv.XoaNhanVien(nhanvien);

                    LoadNhanVien(getPage(page));

                }

                if (e.ColumnIndex == 10)
                {
                    nhanvien = nv.LayTheoId(id);
                    DiaChi dc = dchi.LayTheoId(id);
                    FormThemNhanVien fru = new FormThemNhanVien();

                    FormThemNhanVien.dataDc = dc;
                    FormThemNhanVien.dataNv = nhanvien;

                    fru.FormClosed += new FormClosedEventHandler(fru_FormClosed);
                    fru.ShowDialog();






                }
            }
            catch
            {


            }

        }

        void fru_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadNhanVien(getPage(page));
        }
        public List<NhanVien> getPage(int page)
        {
            var query = from c in nv.LayTatCa()
                           .Skip(nums * page - nums).Take(nums)
                        select c;
            return query.ToList();


        }


        private void bttimnhanvien_Click(object sender, EventArgs e)
        {

        }

        private void btrefresh_Click(object sender, EventArgs e)
        {
            tong = ManagerForm.TongTrang(nv.LayTatCa().Count, nums);
            LoadNhanVien(getPage(page));

        }

        private void btthem_Click(object sender, EventArgs e)
        {
            FormThemNhanVien frs = new FormThemNhanVien();

            FormThemNhanVien.dataDc = null;
            FormThemNhanVien.dataNv = null;

            frs.FormClosed += new FormClosedEventHandler(frs_FormClosed);
            frs.ShowDialog();

        }

        void frs_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadNhanVien(getPage(page));
        }

        private void btfindnhanvien_Click(object sender, EventArgs e)
        {
            FormTimKiemNhanVien fr = new FormTimKiemNhanVien();
            fr.FormClosed += new FormClosedEventHandler(fr_FormClosed);
            fr.ShowDialog();
        }

        void fr_FormClosed(object sender, FormClosedEventArgs e)
        {
            string data = FormTimKiemNhanVien.data;
            if (FormTimKiemNhanVien.state)
            {
                List<NhanVien> dsnhanvien = nv.LayTheoTen(data);
                if (dsnhanvien.Count > 0)
                    LoadNhanVien(dsnhanvien);
                else MessageBox.Show("Không tìm thấy", "Thông Báo");
            }


        }

        private void btntien_Click(object sender, EventArgs e)
        {

            if (page < tong)
                page++;

            if (page <= tong)
            {
                tbdl.Text = page + " / " + tong;
                LoadNhanVien(getPage(page));

            }
        }

        private void btnlui_Click(object sender, EventArgs e)
        {
            if (page >= 2)
            {
                page--;
                tbdl.Text = page + " / " + tong;
                LoadNhanVien(getPage(page));
            }
        }

        private void tbdl_KeyDown(object sender, KeyEventArgs e)
        {
            int t;
            if (e.KeyData == Keys.Enter)
            {
                try
                {
                    t = Convert.ToInt32(tbdl.Text);
                    if (t <= tong && t > 0)
                    {
                        LoadNhanVien(getPage(t));
                        tbdl.Text = t + " / " + tong;
                        page = t;
                    }
                }
                catch (Exception)
                {

                    MessageBox.Show("Nhâp Trang Muốn Đến");
                }

            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormNhanVien_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
                btrefresh.PerformClick();
            if (e.KeyCode == Keys.Escape)
                btnthoat.PerformClick();

            if (e.Control && e.KeyCode == Keys.F)
                btfindnhanvien.PerformClick();


        }

        private void btsapxep_Click(object sender, EventArgs e)
        {
            List<NhanVien> ds;
            if (State)
            {
                
                ds=QuanLiNhanVien.SapXepTang(getPage(page));
                LoadNhanVien(ds);
                State = !State;

            }
            else
            {
             



              ds=  QuanLiNhanVien.SapXepGiam(getPage(page));
                LoadNhanVien(ds);
                State = !State;


            }













        }
    }
}
